package com.example.yyy

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View


class TextProgressCircle @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null)
    : View(context,attrs) {
    private val paintBack = Paint()
    private val paintFore = Paint()
    private val paintText = Paint()
    private var lineWith = 10f
    private var lineColor = Color.GREEN
    private var mTextSize = 50.0f
    private lateinit var rectF: RectF
    private var mProgress = 0

    init {
        //初始化画笔属性
        //抗锯齿
        paintBack.isAntiAlias = true
        paintFore.isAntiAlias = true
        paintText.isAntiAlias = true
        //笔颜色
        paintBack.color = Color.LTGRAY
        paintFore.color = lineColor
        paintText.color = Color.BLUE
        //笔粗细
        paintBack.strokeWidth=lineWith
        paintFore.strokeWidth=lineWith
        paintText.textSize=mTextSize
        //笔样式
        paintBack.style = Paint.Style.STROKE
        paintFore.style = Paint.Style.STROKE
        paintText.style = Paint.Style.FILL

    }

    override fun onDraw(canvas: Canvas?) {
        var mWidth = width;
        var mHeight = height;
        var mPaddingX = (mHeight-mWidth)/2;

        if(mWidth > mHeight){
            mPaddingX = (mWidth-mHeight)/2;
            mWidth = mHeight;
        }

        val halfWidth = mWidth / 6
        rectF= RectF(Rect(halfWidth + mPaddingX, halfWidth, halfWidth * 5 + mPaddingX, halfWidth * 5))
        canvas?.drawArc(rectF, 0f, 360f, false, paintFore)
        paintFore.color = Color.WHITE
        canvas?.drawArc(rectF, 0f, 360f, false, paintBack)
        paintFore.color = lineColor
        canvas?.drawArc(rectF, 90f, 360*mProgress/100f, false, paintFore);
        canvas?.drawText("$mProgress%",halfWidth*3f-20,halfWidth*3f,paintText)
        super.onDraw(canvas)
    }

    fun setProgress(mProgress:Int){
        this.mProgress = mProgress
        invalidate()
    }

}